package com.ruoyi.jiajia.controller;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.JiajiaAppVersion;
import com.ruoyi.system.service.IJiajiaAppVersionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * App版本更新Controller
 * 
 * @author knd
 * @date 2021-10-29
 */
@Controller
@RequestMapping("/jiajia/version")
public class JiajiaAppVersionController extends BaseController
{
    private String prefix = "jiajia/version";

    @Autowired
    private IJiajiaAppVersionService jiajiaAppVersionService;

    @RequiresPermissions("jiajia:version:view")
    @GetMapping()
    public String version()
    {
        return prefix + "/version";
    }

    /**
     * 查询App版本更新列表
     */
    @RequiresPermissions("jiajia:version:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JiajiaAppVersion jiajiaAppVersion)
    {
        startPage();
        List<JiajiaAppVersion> list = jiajiaAppVersionService.selectJiajiaAppVersionList(jiajiaAppVersion);
        return getDataTable(list);
    }

    /**
     * 导出App版本更新列表
     */
    @RequiresPermissions("jiajia:version:export")
    @Log(title = "App版本更新", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JiajiaAppVersion jiajiaAppVersion)
    {
        List<JiajiaAppVersion> list = jiajiaAppVersionService.selectJiajiaAppVersionList(jiajiaAppVersion);
        ExcelUtil<JiajiaAppVersion> util = new ExcelUtil<JiajiaAppVersion>(JiajiaAppVersion.class);
        return util.exportExcel(list, "App版本更新数据");
    }

    /**
     * 新增App版本更新
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存App版本更新
     */
    @RequiresPermissions("jiajia:version:add")
    @Log(title = "App版本更新", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JiajiaAppVersion appVersion)
    {
        if (appVersion.getPlatform().equals("ios")){

        }else {
            if (StringUtils.isEmpty(appVersion.getPath())){
                return error("没有上传apk");
            }
        }

        //查询当前平台版本号是否存在
        JiajiaAppVersion searchVersion = new JiajiaAppVersion();
        searchVersion.setPlatform(appVersion.getPlatform());
        searchVersion.setVersionCode(appVersion.getVersionCode());
        searchVersion.setType(appVersion.getType());
        List<JiajiaAppVersion> appVersionList = jiajiaAppVersionService.selectJiajiaAppVersionList(searchVersion);
        if (!appVersionList.isEmpty()){
            return error(appVersion.getPlatform()+"此版本号已经存在了");
        }

        return toAjax(jiajiaAppVersionService.insertJiajiaAppVersion(appVersion));
    }

    /**
     * 修改App版本更新
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JiajiaAppVersion jiajiaAppVersion = jiajiaAppVersionService.selectJiajiaAppVersionById(id);
        mmap.put("jiajiaAppVersion", jiajiaAppVersion);
        return prefix + "/edit";
    }

    /**
     * 修改保存App版本更新
     */
    @RequiresPermissions("jiajia:version:edit")
    @Log(title = "App版本更新", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JiajiaAppVersion jiajiaAppVersion)
    {
        return toAjax(jiajiaAppVersionService.updateJiajiaAppVersion(jiajiaAppVersion));
    }

    /**
     * 删除App版本更新
     */
    @RequiresPermissions("jiajia:version:remove")
    @Log(title = "App版本更新", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jiajiaAppVersionService.deleteJiajiaAppVersionByIds(ids));
    }
}
